package com.hww.doc.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.hww.doc.business.annotation.BussinessLog;
import com.hww.doc.business.entity.Books;
import com.hww.doc.business.entity.Itemsets;
import com.hww.doc.business.enums.ResponseStatus;
import com.hww.doc.business.service.DocBookService;
import com.hww.doc.business.service.DocItemsetService;
import com.hww.doc.business.vo.BooksConditionVO;
import com.hww.doc.business.vo.ItemsetsConditionVO;
import com.hww.doc.framework.object.PageResult;
import com.hww.doc.framework.object.ResponseVO;
import com.hww.doc.util.ResultUtil;

@RestController
@RequestMapping("/book")
public class RestBookController {
	@Autowired
	DocBookService bookService;
	
	@RequiresPermissions("books")
	@PostMapping("/list")
	public PageResult list(BooksConditionVO vo) {
		PageInfo<Books> pageInfo = bookService.findPageBreakByCondition(vo);
		return ResultUtil.tablePage(pageInfo);
	}
	
	@RequiresPermissions("book:add")
    @PostMapping(value = "/add")
    @BussinessLog("添加项目")
    public ResponseVO add(Books books) {
		books = bookService.insert(books);
        return ResultUtil.success("标签添加成功！新标签 - " + books.getBookName(), books);
    }

    @RequiresPermissions(value = {"book:batchDelete", "book:delete"}, logical = Logical.OR)
    @PostMapping(value = "/remove")
    @BussinessLog("删除项目")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
        	bookService.removeByPrimaryKey(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个标签");
    }

    @RequiresPermissions("book:get")
    @PostMapping("/get/{id}")
    @BussinessLog("获取项目详情")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.bookService.getByPrimaryKey(id));
    }

    @RequiresPermissions("book:edit")
    @PostMapping("/edit")
    @BussinessLog("编辑项目")
    public ResponseVO edit(Books books) {
        try {
        	bookService.updateSelective(books);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("标签修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
    
}
